package com.collection.provider.biz;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.collection.api.entity.sys.SysResource;
import com.collection.api.entity.sys.SysRole;
import com.collection.api.entity.sys.SysRoleResource;
import com.collection.provider.business.dao.sys.SysResourceMapper;
import com.collection.provider.business.dao.sys.SysRoleMapper;

@Service
public class SysRoleBiz {
	
	@Autowired
	private SysRoleMapper sysRoleMapper;
	@Autowired
	private SysResourceMapper sysResourceMapper;
	

	/**
	 * @param 功能描述:查找角色列表
	 */
	public List<Map<String, Object>> findRoleList(Map<String, Object> map){
		return sysRoleMapper.findRoleList(map);
	}
	
    /**
     * 功能描述:统计角色数目
     */
    public int countRoleList(Map<String,Object> map){
    	return sysRoleMapper.countRoleList(map);
    }

    
    /**
     * 功能描述:查找所有资源并根据角色Id将已关联的资源 checked =1 --树
     */
    public List<Map<String, Object>> findResourcesByRoleId(Integer roleId,
                                                           Integer appId) {
        return sysRoleMapper.findResourcesByRoleId(roleId, appId);
    }
    
    /**
     * 功能描述:保存角色
     * @return 
     */
    public void saveRole(String name,
                         String userType,
                         Integer appId,
                         List<String> resourceIds) {
        SysRole role = new SysRole();
        role.setName(name);
        role.setUserType(userType);
        role.setCreateTime(new Date());
        role.setAppId(appId);
        sysRoleMapper.insertSelective(role);
        sysRoleMapper.saveRoleMRessource(resourceIds, role.getId());
    }
    
    /**
     * 功能描述:更新角色
     * @return 
     */
    public void updateRole(Integer roleId,
                           String name,
                           String userType,
                           Integer appId,
                           List<String> resourceIds) {
    	SysRole role = new SysRole();
        role.setId(roleId);
        role.setName(name);
        role.setUserType(userType);
        role.setCreateTime(new Date());
        role.setAppId(appId);
        sysRoleMapper.updateByPrimaryKeySelective(role);
        sysResourceMapper.delRoleMRessourceByRoleId(roleId);
        sysRoleMapper.saveRoleMRessource(resourceIds, role.getId());
    }
    
    /**
     * 功能描述:查找角色
     */
    public SysRole selectByPrimaryKey(int roleId) {
        return sysRoleMapper.selectByPrimaryKey(roleId);
    }
    
    
    /**
     * 根据用户名查找用户个数
     */
    public int countUserByRoleId(int roleId) {
        return sysRoleMapper.countUserByRoleId(roleId);
    }
    
	
    /**
     * 功能描述:删除指定角色的记录及资源与角色中间表记录
     * @return 
     */
    public void deleteRoleAndRoleResourceByRoleId(int roleId) {
    	sysRoleMapper.delRoleByRoleId(roleId);
    	sysResourceMapper.delRoleMRessourceByRoleId(roleId);
    }
    
    /**
     * 功能描述:查找所有角色
     */
    public List<SysRole> findRoleListAll() {
        return sysRoleMapper.findRoleListAll();
    }
    

    
}
